/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.camachosanchez.controler.jdbc;

import com.camachosanchez.bean.Almacenes;
import com.camachosanchez.util.ConexionSQL;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Zully
 */
public class AlmacenesJdbcControler {
    
    public List<Almacenes> findAll(){
        List<Almacenes> lst = new ArrayList<Almacenes>();
        Connection cn = ConexionSQL.obtenerConexion();
        Statement st;
        try {
            st = cn.createStatement();
            ResultSet rs = st.executeQuery("SELECT id, nombre, direccion, telefono, fax, email, encargado, comentarios, estado "
                    + "FROM almacenes WHERE estado=1;");
            while (rs.next()) {
                Almacenes almacen = new Almacenes();
                almacen.setId(rs.getInt(1));
                almacen.setNombre(rs.getString(2));
                almacen.setDireccion(rs.getString(3));
                almacen.setTelefono(rs.getString(4));
                almacen.setFax(rs.getString(5));
                almacen.setEmail(rs.getString(6));
                almacen.setEncargado(rs.getString(7));
                almacen.setComentarios(rs.getString(8));
                almacen.setEstado(rs.getBoolean(9));
                lst.add(almacen);
            }
            rs.close();
            st.close();
            cn.close();
        } catch (SQLException ex) {
            Logger.getLogger(AlmacenesJdbcControler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return lst;
    }
    
    public byte eliminarAlmacen(int id) {
        byte estado = 0;
        try {
            Connection cn = ConexionSQL.obtenerConexion();
            CallableStatement sp = cn.prepareCall("{ call eliminar_almacen(?) }");
            sp.setInt("idAlmacen", id);
            ResultSet rs = sp.executeQuery();
            if(rs.next()){
                estado = rs.getByte(1);
            }
            rs.close();
            sp.close();
            cn.close();
        } catch (SQLException ex) {
            Logger.getLogger(AlmacenesJdbcControler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return estado;
    }
}
